package com.kvest.map.mapview.engine.renderer;

import android.opengl.GLSurfaceView;
import android.util.Log;
import com.kvest.map.mapview.engine.model.AbstractModel;
import com.kvest.map.mapview.engine.model.TileTextureManager;
import com.kvest.map.mapview.engine.utils.ColorUtils;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 21.04.13
 * Time: 16:22
 * To change this template use File | Settings | File Templates.
 */
public class OSMMapRenderer implements GLSurfaceView.Renderer {
    private static final int DEFAULT_Z_NEAR = 1;
    private static final int DEFAULT_Z_FAR = 5;
    private static final int DEFAULT_BACKGROUND_COLOR = 0xffffffff;//0xffb4d0d0;

    private int backgroundColor;
    private boolean backgroundColorChanged;
    private AbstractModel model;

    private OnSurfaceActionListener onSurfaceActionListener;

    public OSMMapRenderer(AbstractModel model) {
        this.model = model;
        backgroundColor = DEFAULT_BACKGROUND_COLOR;
        backgroundColorChanged = true;
        onSurfaceActionListener = null;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        //set gl for texture manager
        TileTextureManager.getInstance().initialize(gl);

        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glDisable(GL10.GL_DITHER);
        gl.glDisable(GL10.GL_DEPTH_TEST);

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        //TODO Попробовать если будет тупить
        //Really Nice Perspective Calculations
        //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

        //emmit event
        if (onSurfaceActionListener != null) {
            onSurfaceActionListener.onSurfaceCreated();
        }
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);

        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        //TODO Возможно надо будет поменять
        gl.glFrustumf(-ratio, ratio, -1, 1, DEFAULT_Z_NEAR, DEFAULT_Z_FAR);

        //emmit event
        if (onSurfaceActionListener != null) {
            onSurfaceActionListener.onSurfaceChanged();
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //set background color if needed
        if (backgroundColorChanged) {
            applyBackgroundColor(gl);
        }

        //Clear all
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

        //Draw model
        model.draw(gl);
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        this.backgroundColorChanged = true;
    }

    public void setOnSurfaceActionListener(OnSurfaceActionListener onSurfaceActionListener) {
        this.onSurfaceActionListener = onSurfaceActionListener;
    }

    private void applyBackgroundColor(GL10 gl) {
        gl.glClearColor(ColorUtils.getRedf(backgroundColor), ColorUtils.getGreenf(backgroundColor),
                ColorUtils.getBluef(backgroundColor), ColorUtils.getAlphaf(backgroundColor));
        backgroundColorChanged = false;
    }
}
